package com.atguigu.lease.web.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
@RequiredArgsConstructor
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
        private final RoomAttrValueService roomAttrValueService;
        private  final RoomFacilityService roomFacilityService;
        private  final RoomLabelService roomLabelService;
        private  final RoomPaymentTypeService roomPaymentTypeService;
        private  final RoomLeaseTermService roomLeaseTermService;
        private final GraphInfoService graphInfoService;
        private  final  RoomInfoMapper roomInfoMapper;
        @Autowired
        private final RedisTemplate<String,Object> redisTemplate;
        //private final stringObjectRedisTemplate
    @Override
    public void saveorupdatenew(RoomSubmitVo roomSubmitVo) {
        boolean isupdate=roomSubmitVo.getId()!=null;
        Long id = roomSubmitVo.getId();
        if(isupdate) {
            //更改公寓表
            RoomInfo roomInfo = BeanUtil.copyProperties(roomSubmitVo, RoomInfo.class);
            saveOrUpdate(roomInfo);
            //删除中间表中数据
            //获取id

            //1
            Db.lambdaUpdate(RoomAttrValue.class)
                    .eq(RoomAttrValue::getRoomId, id)
                    .remove();
            Db.lambdaUpdate(RoomFacility.class)
                    .eq(RoomFacility::getRoomId, id)
                    .remove();
            Db.lambdaUpdate(RoomLabel.class)
                    .eq(RoomLabel::getRoomId, id)
                    .remove();
            Db.lambdaUpdate(RoomPaymentType.class)
                    .eq(RoomPaymentType::getRoomId, id)
                    .remove();
            Db.lambdaUpdate(RoomLeaseTerm.class)
                    .eq(RoomLeaseTerm::getRoomId, id)
                    .remove();
            Db.lambdaUpdate(GraphInfo.class)
                    .eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getItemId, id)
                    .remove();
            //删除缓存
            String key = RedisConstant.APP_ROOM_PREFIX+roomSubmitVo.getId();
            redisTemplate.delete(key);
        }
            //添加数据
            List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
            if (CollUtil.isNotEmpty(attrValueIds)) {
                List<RoomAttrValue> attrValues = new ArrayList<>();
                for (Long attrValueId : attrValueIds) {
                    RoomAttrValue a = new RoomAttrValue();
                    a.setRoomId(id);
                    a.setAttrValueId(attrValueId);
                    attrValues.add(a);
                }
                roomAttrValueService.saveBatch(attrValues);
            }

            List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
            if (CollUtil.isNotEmpty(facilityInfoIds)) {
                List<RoomFacility> roomFacilities = new ArrayList<>();
                for (Long facilityInfoId : facilityInfoIds) {
                    RoomFacility r = new RoomFacility();
                    r.setRoomId(id);
                    r.setFacilityId(facilityInfoId);
                    roomFacilities.add(r);
                }
                roomFacilityService.saveBatch(roomFacilities);
            }

            List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
            if (CollUtil.isNotEmpty(labelInfoIds)) {
                List<RoomLabel> roomLabels = new ArrayList<>();
                for (Long labelInfoId : labelInfoIds) {
                    RoomLabel r = new RoomLabel();
                    r.setRoomId(id);
                    r.setLabelId(labelInfoId);
                    roomLabels.add(r);
                }
                roomLabelService.saveBatch(roomLabels);
            }

            List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
            if (CollUtil.isNotEmpty(paymentTypeIds)) {
                List<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
                for (Long paymentTypeId : paymentTypeIds) {
                    RoomPaymentType p = new RoomPaymentType();
                    p.setPaymentTypeId(paymentTypeId);
                    p.setRoomId(id);
                    roomPaymentTypes.add(p);
                }
                roomPaymentTypeService.saveBatch(roomPaymentTypes);
            }

            List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
            if (CollUtil.isNotEmpty(leaseTermIds)) {
                List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
                for (Long leaseTermId : leaseTermIds) {
                    RoomLeaseTerm r = new RoomLeaseTerm();
                    r.setRoomId(id);
                    r.setLeaseTermId(leaseTermId);
                    roomLeaseTerms.add(r);
                }
                roomLeaseTermService.saveBatch(roomLeaseTerms);
            }

            List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
            if (CollUtil.isNotEmpty(graphVoList)) {
                List<GraphInfo> graphInfos = new ArrayList<>();
                for (GraphVo graphVo : graphVoList) {
                    GraphInfo g = new GraphInfo();
                    g.setItemType(ItemType.ROOM);
                    g.setItemId(id);
                    g.setUrl(graphVo.getUrl());
                    g.setName(graphVo.getName());
                    graphInfos.add(g);
                }
                graphInfoService.saveBatch(graphInfos);
            }
    }

    @Override
    public IPage<RoomItemVo> pageitem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageitem(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomInfo byId = getById(id);
        RoomDetailVo roomDetailVo=new RoomDetailVo();
        BeanUtils.copyProperties(byId,roomDetailVo);
        //获取公寓信息
        Long apartmentId = byId.getApartmentId();
        ApartmentInfo apartmentInfo=Db.lambdaQuery(ApartmentInfo.class)
                .eq(ApartmentInfo::getId,apartmentId).one();
        roomDetailVo.setApartmentInfo(apartmentInfo);
        //图片
        List<GraphVo> graphVoList=new ArrayList<>();
        List<GraphInfo> graphInfos = Db.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.ROOM)
                .list();
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo g=new GraphVo();
            g.setName(graphInfo.getName());
            g.setUrl(graphInfo.getUrl());
            graphVoList.add(g);
        }
        roomDetailVo.setGraphVoList(graphVoList);
        //获取attr集合
        List<AttrValueVo> attrValues = roomInfoMapper.selectarr(id);
        roomDetailVo.setAttrValueVoList(attrValues);
        //获取配套信息
        List<FacilityInfo> facilityInfoList=roomInfoMapper.selectfacility(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        //获取标签信息
        List<LabelInfo> labelInfoList=roomInfoMapper.selectlabel(id);
        roomDetailVo.setLabelInfoList(labelInfoList);
        //获取支付方式
        List<PaymentType> paymentTypeList=roomInfoMapper.getpay(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //获取可选租期列表
        List<LeaseTerm> leaseTermList =roomInfoMapper.getlease(id);
        roomDetailVo.setLeaseTermList( leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void removenew(Long id) {
        removeById(id);
        Db.lambdaUpdate(RoomLabel.class)
                .eq(RoomLabel::getRoomId,id)
                .remove();
        Db.lambdaUpdate(RoomFacility.class)
                .eq(RoomFacility::getRoomId,id)
                .remove();
        Db.lambdaUpdate(RoomLeaseTerm.class)
                .eq(RoomLeaseTerm::getRoomId,id)
                .remove();
        Db.lambdaUpdate(RoomAttrValue.class)
                .eq(RoomAttrValue::getRoomId,id)
                .remove();
        Db.lambdaUpdate(GraphInfo.class)
                .eq(GraphInfo::getItemType,ItemType.ROOM)
                .eq(GraphInfo::getItemId,id)
                .remove();
        Db.lambdaUpdate(RoomPaymentType.class)
                .eq(RoomPaymentType::getRoomId,id)
                .remove();
        //删除缓存
        String key=RedisConstant.APP_ROOM_PREFIX+id;
        redisTemplate.delete(key);
    }
}




